﻿package src.flip{
	import flash.display.*;
	import flash.events.*;
	import flash.filters.*;
	import flash.geom.*;
	import flash.media.*;
	import flash.net.*;
	import flash.system.*;
	import flash.text.*;
	import flash.ui.*;
	import flash.utils.*;
	import src.events.*;
	import src.utils.*;
	import src.caurina.transitions.*;
	public final class Zoomer extends Sprite {
		
		private var flip:Flip = Flip.flip;
		private var config:Object = Flip.config;
		//
		private var zoom_back:Sprite = new Sprite();
		private var zoom_mask:Sprite = new Sprite();
		private var zoom_main:Sprite = new Sprite();
		private var zoomL:Sprite = new Sprite();
		private var zoomR:Sprite = new Sprite();
		//
		private var rect:Rectangle;
		private var offsetX:Number = 0;
		private var offsetY:Number = 0;
		private var showL:Boolean;
		private var showR:Boolean;
		private var srcL:String;
		private var srcR:String;
		//
		private var dragX:Number = 0;
		private var dragY:Number = 0;
		private var clickX:Number = 0;
		private var clickY:Number = 0;
		//
		private var loadedL:Boolean = false;
		private var loadedR:Boolean = false;
		//鼠标指针
		private var cursor:Sprite = new Sprite();
		private var iconzoomout:IconZoomOut;
		private var iconmove:IconMove;
		//正在拖动
		private var draging:Boolean = false;
		//
		public function Zoomer():void {
			this.visible = false;
			//
			addChild(zoom_back);
			addChild(zoom_main);
			zoom_main.addChild(zoomL);
			zoomR.x = config['page_width'];
			zoom_main.addChild(zoomR);
			addChild(zoom_mask);
			zoom_main.mask = zoom_mask;
			//
			iconzoomout = new IconZoomOut();
			iconzoomout.x = iconzoomout.y = -5;
			cursor.addChild(iconzoomout);
			iconmove = new IconMove();
			iconmove.x = iconmove.y = -9;
			iconmove.visible = false;
			cursor.addChild(iconmove);
			cursor.mouseEnabled = false;
			cursor.mouseChildren = false;
			cursor.visible = false;
			addChild(cursor);
			//
			zoom_main.addEventListener(MouseEvent.MOUSE_WHEEL, wheelHandler);
			zoom_main.addEventListener(MouseEvent.MOUSE_OVER, overHandler);
			zoom_main.addEventListener(MouseEvent.MOUSE_OUT, outHandler);
			zoom_main.addEventListener(MouseEvent.MOUSE_MOVE, moveHandler);
			zoom_main.addEventListener(MouseEvent.CLICK, clickHandler);
			zoom_main.addEventListener(MouseEvent.MOUSE_DOWN, downHandler);
			flip.stage.addEventListener(MouseEvent.MOUSE_UP, upHandler);
			//
			flip.addEventListener(FlipEvent.RESIZE, resizeHandler);
			flip.addEventListener(FlipEvent.ZOOMER_LOAD, loadHandler);
			flip.addEventListener(FlipEvent.ZOOMER_SHOW, showHandler);
			flip.addEventListener(FlipEvent.ZOOMER_HIDE, hideHandler);
			flip.addEventListener(FlipEvent.NAV_SEEK, seekHandler);
			resizeHandler();
		}
		//==============================================================================
		private function wheelHandler(e:MouseEvent):void {
			if(e.delta > 0) {
				Flip.sendEvent(FlipEvent.ZOOMER_HIDE, true);
			}
		}
		private function overHandler(e:MouseEvent):void {
			Mouse.hide();
			moveHandler();
			cursor.visible = true;
		}
		private function outHandler(e:MouseEvent):void {
			Mouse.show();
			cursor.visible = false;
		}
		private function moveHandler(e:MouseEvent = null):void {
			if (draging) {
				Mouse.hide();
				iconzoomout.visible = false;
				iconmove.visible = true;
				//
				var zx:Number = mouseX - dragX * config['zoom'];
				var zy:Number = mouseY - dragY * config['zoom'];
				var minX:Number = - config['page_width'] * (config['zoom'] - 1) * 2;
				var maxX:Number = 0;
				var minY:Number = - config['page_height'] * (config['zoom'] - 1);
				var maxY:Number = 0;
				if (!showL && showR) {
					maxX = - config['page_width'] * (config['zoom'] - 1);
				} else if (showL && !showR) {
					minX = - config['page_width'] * (config['zoom'] - 1);
				}
				zoom_main.x = Flip.fitNumber(zx, minX, maxX);
				zoom_main.y = Flip.fitNumber(zy, minY, maxY);
				navHandler();
			}
			//
			cursor.x = mouseX;
			cursor.y = mouseY;
		}
		
		private function clickHandler(e:MouseEvent):void {
			if (clickX == stage.mouseX && clickY == stage.mouseY) {
				Flip.sendEvent(FlipEvent.ZOOMER_HIDE, true);
			}
		}
		private function downHandler(e:MouseEvent):void {
			draging = true;
			//
			clickX = stage.mouseX;
			clickY = stage.mouseY;
			dragX = zoom_main.mouseX;
			dragY = zoom_main.mouseY;
			//trace("dragX:" + dragX + "|dragY:" + dragY);
		}

		private function upHandler(e:MouseEvent):void {
			iconzoomout.visible = true;
			iconmove.visible = false;
			draging = false;
		}
		//==============================================================================
		private function loadHandler(e:FlipEvent):void {
			//return;
			var dat:Object = e.data;
			showL = dat.showL;
			showR = dat.showR;
			srcL = dat.srcL;
			srcR = dat.srcR;
			//trace("showL:" + showL + "|showR:" + showR + "|srcL:" + srcL + "|srcR:" + srcR);
			//
			loadedL = false;
			loadedR = false;
			Flip.clearPages(zoomL);
			Flip.clearPages(zoomR);
			zoom_main.x = zoom_main.y = 0;
			zoom_main.scaleX = zoom_main.scaleY = 1;
			if (!showL && showR) {
				rect = new Rectangle(config['page_width'], 0, config['page_width'], config['page_height']);
				loadR();
			} else if (showL && !showR) {
				rect = new Rectangle(0, 0, config['page_width'], config['page_height']);
				loadL();
			} else {
				rect = new Rectangle(0, 0, config['page_width'] * 2, config['page_height']);
				loadL();
				loadR();
			}
			Flip.drawRect(zoom_mask, rect);
			//
			Flip.drawRect(zoom_back, rect);
		}
		
		private function showHandler(e:FlipEvent):void {
			this.visible = true;
			var dat:Object = e.data;
			offsetX = dat.offsetX;
			offsetY = dat.offsetY;
			//trace("offsetX:" + offsetX + "|offsetY:" + offsetY);
			var scale:Number = config['zoom'];
			//
			var px:Number = - offsetX * (scale - 1);
			var py:Number = - offsetY * (scale - 1);
			Tweener.addTween(zoom_main, {x:px, y:py, scaleX:scale, scaleY:scale, time:config['speed'], transition:"easeOutCubic", onComplete:navHandler});
		}
		private function navHandler():void {
			var sx:Number = zoom_main.x / zoom_main.width;
			var sy:Number = zoom_main.y / zoom_main.height;
			var scale:Number = config['zoom'];
			if (!showL && showR) {
				sx = (zoom_main.x + zoom_main.width * 0.5 - config['page_width']) / (zoom_main.width * 0.5);
			} else if (showL && !showR) {
				sx = zoom_main.x / (zoom_main.width * 0.5);
			}
			Flip.sendEvent(FlipEvent.ZOOMER_SEEK, {sx:sx, sy:sy});
		}
		
		private function hideHandler(e:FlipEvent):void {
			if (e.data) {
				Tweener.addTween(zoom_main, {scaleX:1, scaleY:1, x:0, y:0, time:config['speed'], transition:"easeOutCubic", onComplete:zoomoutHandler});
			} else {
				this.visible = false;
			}
		}
		private function zoomoutHandler():void {
			this.visible = false;
		}
		
		private function seekHandler(e:FlipEvent):void {
			var sx:Number = e.data.sx;
			var sy:Number = e.data.sy;
			//trace(sx + "|" + sy);
			var tx:Number = -sx * zoom_main.width;
			var ty:Number = -sy * zoom_main.height;
			if (!showL && showR) {
				tx = -sx * zoom_main.width * 0.5 + config['page_width'] - zoom_main.width * 0.5;
			} else if (showL && !showR) {
				tx = -sx * zoom_main.width * 0.5;
			}
			zoom_main.x = tx;
			zoom_main.y = ty;
		}

		private function loadL():void {
			var page:Page = new Page(srcL);
			page.load();
			zoomL.addChild(page);
			loadedL = true;
			finished();
		}
		//
		private function loadR():void {
			var page:Page = new Page(srcR);
			page.load();
			zoomR.addChild(page);
			loadedR = true;
			finished();
		}
		
		private function finished():void {
			if ((!showL && showR && loadedR) || (showL && !showR && loadedL) || (showL && showR && loadedL && loadedR)) {
				Flip.drawRect(zoom_main, getRect(zoom_main));
			} 
		}
		
		//=======================================================================================================
		private function resizeHandler(e:FlipEvent = null):void {
			this.x = config['width'] * 0.5 - config['page_width'];
			this.y = (config['height'] - config['page_height'] - Flip.bar.bar_back.height) * 0.5;
		}
		
	}
}